package cn.edu.zut.hhr.controller;

import cn.edu.zut.hhr.entity.Dept;
import cn.edu.zut.hhr.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;


@Controller
@RequestMapping("/dept")

public class DeptController{

    @Autowired
    RestTemplate restTemplate;

    //因为请求路径肯定有公共前缀
    //public static final String PATH = "http://localhost:8081";
    public static final String PRE_PATH = "http://";
    //因为采用了负载均衡所以肯定不能指定一个访问了,改成applicationName,当然,别硬编码了,从配置文件里找吧
    @Value("${spring.application.name}")
    private String PATH;

    @GetMapping("/{id}")
    @ResponseBody
    public Dept getById(@PathVariable("id") long id){
        String url = PRE_PATH + PATH + "/dept/"+ id;
        return restTemplate.getForObject(url,Dept.class);
    }

}
